package com.photoeditor.demo.ui.widget.gallery;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.photoeditor.R;
import com.photoeditor.demo.imageloader.ImageLoader;
import com.photoeditor.demo.model.bean.ThumbnailBean;
import com.photoeditor.demo.util.MediaTypeUtil;
import com.photoeditor.demo.util.folder.FolderHelper;
import com.photoeditor.demo.util.gallery.common.LoadThumbnailUtil;

/**
 * 相册页面一个单元格
 * <p>
 * Created by winn on 18/1/19.
 */

public class GalleryCellItem extends RelativeLayout {
    protected ImageView mLoadingImageView;
    private ImageView mImageView;
    private ImageView mPlayImageView;
    private ImageView mSignImageView;
    private ImageView mCheckbox;
    private View mSelMask;
    private View mSelBg;
    private TextView mSelNum;

    private RelativeLayout mAdContainer;

    private Object mData;
    private OnItemListener mListener;

    public GalleryCellItem(Context context) {
        super(context);
        init();
    }

    public GalleryCellItem(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public GalleryCellItem(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public void setOnItemListener(OnItemListener listener) {
        mListener = listener;
    }

    private void init() {
        inflate(getContext(), R.layout.layout_gallery_cell_item, this);
        mLoadingImageView = findViewById(R.id.imgLoading);
        mImageView = findViewById(R.id.img);
        mPlayImageView = findViewById(R.id.play_icon);
        mSignImageView = findViewById(R.id.sign_icon);
        mCheckbox = findViewById(R.id.check_box);
        mSelMask = findViewById(R.id.sel_mask);
        mSelBg = findViewById(R.id.sel_bg);
        mSelNum = findViewById(R.id.sel_num);

        mAdContainer = findViewById(R.id.ad_container);
        setListener();
    }

    public void updateData(Object object, boolean picMode) {
        mData = object;
        if (object instanceof ThumbnailBean) {
            mAdContainer.setVisibility(INVISIBLE);
            ThumbnailBean bean = (ThumbnailBean) mData;
            if (MediaTypeUtil.isVideo(bean.getType())) {
                if (picMode || FolderHelper.isMotionFilePath(bean.getPath())) {
                    mSignImageView.setVisibility(VISIBLE);
                    mPlayImageView.setVisibility(INVISIBLE);
                    mSignImageView.setImageBitmap(LoadThumbnailUtil.getInstance().getDynamicVideoSignBitmap());
                } else {
                    mSignImageView.setVisibility(INVISIBLE);
                    mPlayImageView.setVisibility(VISIBLE);
                }
            } else if (MediaTypeUtil.isGif(bean.getType())) {
                mSignImageView.setVisibility(VISIBLE);
                mPlayImageView.setVisibility(INVISIBLE);
                mSignImageView.setImageBitmap(LoadThumbnailUtil.getInstance().getGIFSignBitmap());
            } else {
                mSignImageView.setVisibility(INVISIBLE);
                mPlayImageView.setVisibility(INVISIBLE);
            }
            setSelected(bean.isChecked());
            if (picMode) {
                if (bean.getSelectCount() > 0) {
                    mSelBg.setVisibility(VISIBLE);
                    mSelNum.setVisibility(VISIBLE);
                    mSelNum.setText(String.valueOf(bean.getSelectCount()));
                } else {
                    mSelBg.setVisibility(INVISIBLE);
                    mSelNum.setVisibility(INVISIBLE);
                }
            } else {
                mSelBg.setVisibility(INVISIBLE);
                mSelNum.setVisibility(INVISIBLE);
            }
            if (bean.getUri() != null) {
                ImageLoader.loadImage((Activity) getContext(), bean.getUri().toString(), -1, mImageView);
            }
        }
    }

    private void setListener() {
        mPlayImageView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null && mData != null && mData instanceof ThumbnailBean) {
                    mListener.onPlayClick(GalleryCellItem.this, (ThumbnailBean) mData);
                }
            }
        });
        mPlayImageView.setOnLongClickListener(mOnLongClickListener);
        mPlayImageView.setOnTouchListener(mOnTouchListener);
        mImageView.setOnLongClickListener(mOnLongClickListener);
        mImageView.setOnTouchListener(mOnTouchListener);
        mImageView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null && mData != null && mData instanceof ThumbnailBean) {
                    mListener.onImageClick(GalleryCellItem.this, (ThumbnailBean) mData);
                }
            }
        });
    }

    private OnLongClickListener mOnLongClickListener = new OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            if (mListener != null && mData != null && mData instanceof ThumbnailBean) {
                mListener.onImageLongClick(GalleryCellItem.this, (ThumbnailBean) mData);
            }
            return true;
        }
    };

    private OnTouchListener mOnTouchListener = new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (mListener != null && mData != null && mData instanceof ThumbnailBean) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    mListener.onDown(GalleryCellItem.this, (ThumbnailBean) mData);
                } else if (event.getAction() == MotionEvent.ACTION_UP
                        || event.getAction() == MotionEvent.ACTION_CANCEL) {
                    mListener.onUp(GalleryCellItem.this, (ThumbnailBean) mData);
                }
            }
            return false;
        }
    };


    public void setSelected(boolean selected) {
        mCheckbox.setSelected(selected);
        if (selected) {
            mSelMask.setVisibility(VISIBLE);
            mCheckbox.setVisibility(VISIBLE);
        } else {
            mSelMask.setVisibility(INVISIBLE);
            mCheckbox.setVisibility(INVISIBLE);
        }
    }

    public Object getData() {
        return mData;
    }

    public Bitmap getThumbnail() {
        if (mImageView.getDrawable() instanceof BitmapDrawable) {
            return ((BitmapDrawable) mImageView.getDrawable()).getBitmap();
        }
        return null;
    }

    public interface OnItemListener {
        void onImageClick(GalleryCellItem view, ThumbnailBean bean);

        void onPlayClick(GalleryCellItem view, ThumbnailBean bean);

        void onImageLongClick(GalleryCellItem view, ThumbnailBean bean);

        void onDown(GalleryCellItem view, ThumbnailBean bean);

        void onUp(GalleryCellItem view, ThumbnailBean bean);
    }
}
